package com.dl.exam.controller;

import com.alibaba.fastjson.JSONObject;
import com.dl.exam.annotation.MethodInfo;
import com.dl.exam.service.CommonService;
import com.dl.exam.util.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2022-04-21 20:33
 */
@Slf4j
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Resource
    private CommonService commonService;

    @PostMapping("/")
    private Result register(@RequestBody JSONObject jsonObject) {

        JSONObject registerForm = jsonObject.getJSONObject("registerForm");
        String username = registerForm.getString("username");
        String password = registerForm.getString("password");
        boolean register = commonService.register(username, password);
        if (register){
            return Result.ok("注册成功");
        }else {
            return Result.error("注册失败");
        }
    }


    //region 检查用户名是否重复
    @GetMapping("/check")
    private Result check(String username) {
        //判断用户名是否重复
        boolean check = commonService.checkUsername(username);
        if (check){
            return Result.ok();
        }else {
            return Result.no("用户名重复");
        }
    }
}
